<?php
/**
 * User: jintao Date: 2016/7/6
 */
class erpapi_logistics_matrix_hqepay_request_electron extends erpapi_logistics_request_electron
{

    public function bufferRequest($sdf){
        return $this->directNum;
    }

    public function directRequest($sdf){
        $delivery = $sdf['delivery'];
        $shopInfo = $sdf['shop'];

        $to_address = $delivery['ship_addr'] ? $delivery['ship_province'] . $delivery['ship_city'] . $delivery['ship_district'] . $delivery['ship_addr'] : '_SYSTEM';
        $from_address = $shopInfo['address_detail'] ? $shopInfo['province'] . $shopInfo['city'] . $shopInfo['area'] . $shopInfo['address_detail'] : '_SYSTEM';

        $params = array(
            "member_id"=>$delivery['member_id'],
            "send_site"=>"",  # 收件网点标识
            "company_code"=>$this->__channelObj->channel['logistics_code'],  # 物流公司编码
            "logistic_code"=>'',  # 运单号
            "tid"=>$delivery['delivery_bn'],  # 订单号
            "exp_type"=>'1',  # 快递类型 1是标准快件
            "cost"=>'',
            "other_cost"=>'',

            'to_company'=>$this->charFilter($delivery['ship_name']),
            'to_name'=>$delivery['ship_name'],#收货人
            'to_tel'=>$delivery['ship_tel'],
            'to_mobile'=>$delivery['ship_mobile'],
            'to_zip'=>$delivery['ship_zip'],
            'to_province'=>$delivery['ship_province'],
            'to_city'=>$delivery['ship_city'],
            'to_area'=>$delivery['ship_district'],
            'to_address'=> $this->charFilter($to_address),

            'from_company'=>'',
            'from_name'=> $shopInfo['default_sender'] ? $shopInfo['default_sender'] : '_SYSTEM',
            'from_tel'=>$shopInfo['tel'],
            'from_mobile'=> $shopInfo['mobile'],
            'from_zip'=>$shopInfo['zip'],
            'from_province'=>$shopInfo['province'],
            'from_city'=>$shopInfo['city'],
            'from_area'=>$shopInfo['area'],
            'from_address'=>$this->charFilter($from_address),#发件人详细地址


            "start_date"=>'',#上门取货时间段
            "end_date"=>'',
            "weight"=>'',
            "volume"=>'',
            "remark"=>'',
            "qty"=>"",
            "service_list"=>'',#增值服务
            "goods_list"=>json_encode($this->format_delivery_item($sdf['delivery_item'])),#货品明细信息
            "is_return"=>'0',  # 返回电子面单模板：0-不需要；1-需要
        );
        $back =   $this->requestCall(STORE_HQEPAY_ORDERSERVICE, $params);
        return $this->backToResult($back, $delivery);
    }
    #获取货物名称
    public function format_delivery_item(&$deliveryItems = null) {
        $items = array();
        foreach($deliveryItems as $key=>$item){
            $items[$key]['bn'] = $item['bn'];
            $items[$key]['name'] = $item['product_name'];
            $items[$key]['qty'] = $item['number'];
        }
        return $items;
    }
    public function backToResult($back, $delivery){
        $data = empty($back['data']) ? '' : json_decode($back['data'], true);
        if(empty($data)) {
            return false;
        }
        $result = array();
        $result[] = array(
            'succ' => $data['Order'] ['LogisticCode']? true : false,
            'msg' => '',
            'delivery_id' => $delivery['delivery_id'],
            'delivery_bn' => $delivery['delivery_bn'],
            'logi_no' => $data['Order'] ['LogisticCode'],
            'noWayBillExtend' => true,
        );
        return $result;
    }
}